﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace Repositories.UoW
{
    public class SessionFactory
    {
        private static ISessionFactory _sessionFactory;
        public static void Init()
        {
            Configuration cfg=new Configuration();
            cfg.Configure(@".\Data.cfg.xml");
            _sessionFactory = cfg.BuildSessionFactory();
        }

        private static ISession ThreadSession
        {
            get { return (ISession) CallContext.GetData("THREAD_SESSION"); }
            set { CallContext.SetData("THREAD_SESSION",value); }
        }

        public static ISession GetSession()
        {
            if (ThreadSession==null)
            {
                ThreadSession = _sessionFactory.OpenSession();
            }
            return ThreadSession;
        }

    }
}
